package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;


    @PostMapping
    public Object save(Travel travel){
        travel.setAuthor(UserContext.getUserAttribute());
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @PostMapping("{travelId}")
    public JSONResult updateBrowseNum(@PathVariable Long travelId){
        travelService.updateBrowseNum(travelId);
        return new JSONResult();
    }

    @GetMapping("{id}")
    public Object getByUserId(@PathVariable("id")Long id){
        return travelService.getByUserId(id);
}

    @GetMapping
    public Object getRelease(TravelQueryObject qo){
        qo.setState(Travel.TRAVEL_RELEASE);
        qo.setOrderBy("t.releaseTime desc");
        return travelService.queryForRelease(qo);
    }
    @PutMapping("{id}")
    public Object update(Travel travel){
        travel.setAuthor(UserContext.getUserAttribute());
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @GetMapping("{state}/tags")
    public Object queryTrave(TravelQueryObject qo){
        return travelService.queryByStrategyId(qo);
    }

    @GetMapping("/content/{authorId}")
    public Integer queryTravelContentNum(@PathVariable Long authorId){
      return   travelService.queryTravelContentNum(authorId);
    }

}
